package actions;

import java.io.File;
import java.util.List;
import java.util.Map;

import model.VadeMecumMultimedia;
import model.XMLUtil;

import org.apache.commons.io.FileUtils;

import persistence.VadeMecumMultimediaDAO;

import com.opensymphony.xwork2.ActionSupport;

public class QueryUsingXMLFileAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	
	private File xmlFile;
	
	private final String localForCopy = "C:\\Documents and Settings\\winxp\\workspace\\VadeMecumMultimediaService\\teste";
	
	private String contexts; 
	
	//TODO sera VadeMecumMultimedia ou os Files ou os paths??
	private List<VadeMecumMultimedia> results;
	
	private Map<String, Map<String, String>> query;
	
	public String execute() throws Exception {

		try {
			if (xmlFile != null){
				String tempName = "tempQuery.xml";
				String fileNameTemp = localForCopy + tempName;
				File tempFile = new File(fileNameTemp);
				
				System.out.println("tempFileAbsolutePath: " + tempFile.getAbsolutePath());
				
				//Falta apagar a copia do arquivo! TODO
				FileUtils.copyFile(xmlFile, tempFile);

				query = XMLUtil.getInstance().translate(fileNameTemp, contexts);
				System.out.println("Query: " + query.toString());

//				results = VadeMecumMultimediaDAO.getInstance().queryAboutContext(getQuery());
				//TODO
				System.out.println("Resultado da query: " + results.toString()); 
				tempFile.delete();
			}
		} catch (Exception e) {
			
			addActionError(e.getMessage());

			return INPUT;

		}

		return SUCCESS;

	}


	public File getXmlFile() {
		return xmlFile;
	}


	public void setXmlFile(File xmlFile) {
		this.xmlFile = xmlFile;
	}


	public List<VadeMecumMultimedia> getResults() {
		return results;
	}


	public void setResults(List<VadeMecumMultimedia> results) {
		this.results = results;
	}


	public String getContexts() {
		return contexts;
	}


	public void setContexts(String contexts) {
		this.contexts = contexts;
	}


	public Map<String, Map<String, String>> getQuery() {
		return query;
	}


	public void setQuery(Map<String, Map<String, String>> query) {
		this.query = query;
	}


}